package com.hmall.cart.listener;

import com.hmall.cart.service.ICartService;
import com.hmall.common.utils.UserContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
@Slf4j
public class CartStatusListener {

    private final ICartService cartService;


    @RabbitListener(bindings = @QueueBinding(
            value=@Queue(name="cart.clear.queue", durable="true"),
            exchange = @Exchange(name="trade.topic"),
            key = "order.create"
    ) )
    public void listenOrderSuccess(List<Long> ids) {
        Long userId = UserContext.getUser();
        log.info("订单号一创建，正在清除用户{}的购物车",userId);
        cartService.removeByItemIds(ids);
    }

}
